// Datastores.cpp : Implementation of CDatastores

#include "stdafx.h"
#include "Datastores.h"
#include "Values.h"
#include "Datastore.h"

// CDatastores

STDMETHODIMP CDatastores::InterfaceSupportsErrorInfo(REFIID riid)
{
	static const IID* arr[] = 
	{
		&IID_IDatastores
	};

	for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		if (InlineIsEqualGUID(*arr[i],riid))
			return S_OK;
	}
	return S_FALSE;
}

STDMETHODIMP CDatastores::Connect(BSTR ProductName, IDatastore** ppVal)
{
	CComObject<CDatastore> * pDs = GetComObject<CDatastore>();
	pDs->m_pDBHelper = new CDBHelper(ProductName);
	*ppVal = pDs;
	return S_OK;
}

STDMETHODIMP CDatastores::get_DSCatalog(IValues** pVal)
{
	CComObject<CValues> * pVals = GetComObject<CValues>();
	CComSafeArray<BSTR> safeArray;
	if (FAILED(CDBHelper::GetConnectingStrings(safeArray)))
	{
		BSTR err = GetLastErrorString();
		Error(err);
		return E_FAIL;
	}

	LONG alIndex[] = {0, 0};
	
	ULONG count = safeArray.GetCount(1);
	for(ULONG i=0; i<count; i++)
	{
		BSTR name;
		safeArray.MultiDimGetAt(alIndex, name);
		pVals->Add(name);
		alIndex[1]++;
	}
	*pVal = pVals;
	return S_OK;
}

STDMETHODIMP CDatastores::get_Security(IServerSecurity** pVal)
{
    IServerSecurity * pX;
	CoGetCallContext(IID_IServerSecurity, (void **)&pX);
    *pVal = pX;
	return S_OK;
}

STDMETHODIMP CDatastores::S(INT * pVal)
{

	return S_OK;
}